
//% color="#78c030" iconWidth=50 iconHeight=40
namespace esp32OAT {

    //% block="setVersionNumber[V]" blockType="command"
    //% V.shadow="String" V.defl="1.0.1"
    export function setVersionNumber(parameter: any, block: any) {
        let v = parameter.V.code
        Generator.addInclude('XYXKC_OTA_INCLUDE', '#include <XYXKC_OTA.h>');
        Generator.addObject(`XYXKC_OTA_Object`,'XYXKC_OTA','xyxkcOTA;')
        Generator.addObject(`XYXKC_OTA_Object_ret`,'volatile bool','xyxkc_ESP32httpUpdate_ret = false;')
        Generator.addCode(`xyxkcOTA.setVersion("XYXKC-"+String(${v}));`)
    }
    //% block="getVersion" blockType="reporter"
    export function getVersion(parameter: any, block: any) {

        Generator.addCode(`xyxkcOTA.getVersion()`)
    }
    //% block="toTheServer[URL]requestFirmwareUpdate" blockType="command"
    //% URL.shadow="String" URL.defl="http://www.fjqnzj.cn/esp32/api/v1/file/getfile"

    export function UpdateOATUrl(parameter: any, block: any) {
        let url = parameter.URL.code

        Generator.addInclude('XYXKC_OTA_INCLUDE', '#include <XYXKC_OTA.h>');
        Generator.addObject(`XYXKC_OTA_Object`,'XYXKC_OTA','xyxkcOTA;')
        Generator.addObject(`XYXKC_OTA_Object_ret`,'volatile bool','xyxkc_ESP32httpUpdate_ret = False;')
        if(Generator.board === 'esp32'|| Generator.board === 'firebeetleesp32e'){//如果是掌控板，生成如下代码
            Generator.addObject(`XYXKC_OTA_Object_pro`,'void','xyxkc_printProgress(size_t progress, const size_t &size) {\n' +
                '  static int last_progress = -1;\n' +
                '  if (size > 0) {\n' +
                '    progress = (progress * 100) / size;\n' +
                '    progress = (progress > 100 ? 100 : progress);  //0-100\n' +
                '    if (progress != last_progress) {\n' +
                '       display.setCursor(22, 22);\n'+
                '       display.print("升级进度:"+String(progress)+"%");\n'+
                '      last_progress = progress;\n' +
                '    }\n' +
                '  }\n' +
                '}')
        }else{
            Generator.addObject(`XYXKC_OTA_Object_pro`,'void','xyxkc_printProgress(size_t progress, const size_t &size) {\n' +
                '  static int last_progress = -1;\n' +
                '  if (size > 0) {\n' +
                '    progress = (progress * 100) / size;\n' +
                '    progress = (progress > 100 ? 100 : progress);  //0-100\n' +
                '    if (progress != last_progress) {\n' +
                '      Serial.printf("Progress: %d%%\\n", progress);\n' +
                '      last_progress = progress;\n' +
                '    }\n' +
                '  }\n' +
                '}')
        }
        Generator.addSetup(`XYXKC_OTA_Setup`,'xyxkcOTA.onProgress(xyxkc_printProgress);')
        Generator.addCode(`xyxkc_ESP32httpUpdate_ret = xyxkcOTA.update(${url});`)
    }
    //% block="toTheServer[URL]Token[TOKEN]requestFirmwareUpdate" blockType="command"
    //% URL.shadow="String" URL.defl="http://www.fjqnzj.cn/esp32/api/v1/file/getfile"
    //% TOKEN.shadow="String" TOKEN.defl="token"
    export function UpdateOATToken(parameter: any, block: any) {
        let url = parameter.URL.code
        let token = parameter.TOKEN.code
        Generator.addInclude('XYXKC_OTA_INCLUDE', '#include <XYXKC_OTA.h>');
        Generator.addObject(`XYXKC_OTA_Object`,'XYXKC_OTA','xyxkcOTA;')
        Generator.addObject(`XYXKC_OTA_Object_ret`,'volatile bool','xyxkc_ESP32httpUpdate_ret = False;')
        if(Generator.board === 'esp32'){//如果是掌控板，生成如下代码
            Generator.addObject(`XYXKC_OTA_Object_pro`,'void','xyxkc_printProgress(size_t progress, const size_t &size) {\n' +
                '  static int last_progress = -1;\n' +
                '  if (size > 0) {\n' +
                '    progress = (progress * 100) / size;\n' +
                '    progress = (progress > 100 ? 100 : progress);  //0-100\n' +
                '    if (progress != last_progress) {\n' +
                '       display.setCursor(22, 22);\n'+
                '       display.print("升级进度:"+String(progress)+"%");\n'+
                '      last_progress = progress;\n' +
                '    }\n' +
                '  }\n' +
                '}')
        }else{
            Generator.addObject(`XYXKC_OTA_Object_pro`,'void','xyxkc_printProgress(size_t progress, const size_t &size) {\n' +
                '  static int last_progress = -1;\n' +
                '  if (size > 0) {\n' +
                '    progress = (progress * 100) / size;\n' +
                '    progress = (progress > 100 ? 100 : progress);  //0-100\n' +
                '    if (progress != last_progress) {\n' +
                '      Serial.printf("Progress: %d%%\\n", progress);\n' +
                '      last_progress = progress;\n' +
                '    }\n' +
                '  }\n' +
                '}')
        }
        Generator.addSetup(`XYXKC_OTA_Setup`,'xyxkcOTA.onProgress(xyxkc_printProgress);')
        Generator.addCode(`xyxkc_ESP32httpUpdate_ret = xyxkcOTA.update(${url},String(${token}));`)
    }
    //% block="toTheServer[URL]account[NAME]password[PASS]requestFirmwareUpdate" blockType="command"
    //% URL.shadow="String" URL.defl="http://www.fjqnzj.cn/esp32/api/v1/file/getfile"
    //% NAME.shadow="String" NAME.defl="username"
    //% PASS.shadow="String" PASS.defl="password"
    export function UpdateOAT(parameter: any, block: any) {
        let url = parameter.URL.code
        let username = parameter.NAME.code
        let password = parameter.PASS.code
        Generator.addInclude('XYXKC_OTA_INCLUDE', '#include <XYXKC_OTA.h>');
        Generator.addObject(`XYXKC_OTA_Object`,'XYXKC_OTA','xyxkcOTA;')
        Generator.addObject(`XYXKC_OTA_Object_ret`,'volatile bool','xyxkc_ESP32httpUpdate_ret = False;')
        if(Generator.board === 'esp32'){//如果是掌控板，生成如下代码
            Generator.addObject(`XYXKC_OTA_Object_pro`,'void','xyxkc_printProgress(size_t progress, const size_t &size) {\n' +
                '  static int last_progress = -1;\n' +
                '  if (size > 0) {\n' +
                '    progress = (progress * 100) / size;\n' +
                '    progress = (progress > 100 ? 100 : progress);  //0-100\n' +
                '    if (progress != last_progress) {\n' +
                '       display.setCursor(22, 22);\n'+
                '       display.print("升级进度:"+String(progress)+"%");\n'+
                '      last_progress = progress;\n' +
                '    }\n' +
                '  }\n' +
                '}')
        }else{
            Generator.addObject(`XYXKC_OTA_Object_pro`,'void','xyxkc_printProgress(size_t progress, const size_t &size) {\n' +
                '  static int last_progress = -1;\n' +
                '  if (size > 0) {\n' +
                '    progress = (progress * 100) / size;\n' +
                '    progress = (progress > 100 ? 100 : progress);  //0-100\n' +
                '    if (progress != last_progress) {\n' +
                '      Serial.printf("Progress: %d%%\\n", progress);\n' +
                '      last_progress = progress;\n' +
                '    }\n' +
                '  }\n' +
                '}')
        }
        Generator.addSetup(`XYXKC_OTA_Setup`,'xyxkcOTA.onProgress(xyxkc_printProgress);')
        Generator.addCode(`xyxkc_ESP32httpUpdate_ret = xyxkcOTA.update(${url},String(${username}),String(${password}));`)
    }
    //% block="update successful？" blockType="boolean"
    export function updateSuccessful(parameter: any, block: any) {
        Generator.addObject(`XYXKC_OTA_Object`,'XYXKC_OTA','xyxkcOTA;')
        Generator.addObject(`XYXKC_OTA_Object_ret`,'volatile bool','xyxkc_ESP32httpUpdate_ret = False;')
        Generator.addCode(`xyxkc_ESP32httpUpdate_ret`)
    }

    //% block="getErrorInformation" blockType="reporter"
    export function getErrorInformation (parameter: any, block: any) {
        Generator.addObject(`XYXKC_OTA_Object`,'XYXKC_OTA','xyxkcOTA;')
        Generator.addObject(`XYXKC_OTA_Object_ret`,'volatile bool','xyxkc_ESP32httpUpdate_ret = False;')
        Generator.addCode(`xyxkcOTA.getError()`)
    }
    //% block="restart" blockType="command"
    export function restart(parameter: any, block: any) {
        Generator.addCode(`ESP.restart();`)
    }
}